package Web.Servlet;

import Domain.*;
import Service.CommentService;
import Service.Impl.CommentServiceImpl;
import Service.Impl.ProductServiceImpl;
import Service.Impl.UserServiceImpl;
import Service.ProductService;
import Service.UserService;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@WebServlet("/ProductServlet")
public class ProductServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String action = request.getParameter("action");

        if(action.equals("IntoProduct")){
            String pid = request.getParameter("pid");
            String uid = request.getParameter("uid");

            ProductService service = new ProductServiceImpl();
            List<Product> list = service.FindProductById(Integer.parseInt(pid));
            Product product = list.get(0);

            CommentService service1 = new CommentServiceImpl();
            List<Comment> comments = service1.FindCommentByPid(Integer.parseInt(pid));

            if(!uid.equals("")){
                UserService service2 = new UserServiceImpl();
                List<User> list1 = service2.findUserById(Integer.parseInt(uid));
                User user = list1.get(0);
                request.setAttribute("user",user);
            }

            request.setAttribute("product",product);
            request.setAttribute("comment",comments);
            request.getRequestDispatcher("Product/Details.jsp").forward(request,response);
        }
        if (action.equals("findMainTypeById")){
            String pid = request.getParameter("pid");
            String uid = request.getParameter("uid");

            ProductService service = new ProductServiceImpl();
            List<Product> productById = service.FindProductByTypeid(Integer.parseInt(pid));


            if(!uid.equals("")){
                UserService service2 = new UserServiceImpl();
                List<User> list1 = service2.findUserById(Integer.parseInt(uid));
                User user = list1.get(0);
                request.setAttribute("user",user);
            }

            request.setAttribute("pp",productById);

            request.getRequestDispatcher("Index2.jsp").forward(request,response);
        }
        if(action.equals("search")){
            String uid = request.getParameter("uid");
            String search = request.getParameter("main_name");

            ProductService service = new ProductServiceImpl();
            List<Product> productByName = service.likeProduct(search);



            if(!uid.equals("")){
                UserService service2 = new UserServiceImpl();
                List<User> list1 = service2.findUserById(Integer.parseInt(uid));
                User user = list1.get(0);
                request.setAttribute("user",user);
            }

            request.setAttribute("pp",productByName);
            request.getRequestDispatcher("Index3.jsp").forward(request,response);
        }
        if(action.equals("FindByPage")){
            String currentPage = request.getParameter("currentPage");
            String rows = request.getParameter("rows");

            if (currentPage==null || "".equals(currentPage)){
                currentPage="1";
            }
            if(rows==null || "".equals(rows)){
                rows="5";
            }

            Map<String, String[]> condition = request.getParameterMap();

            ProductService service = new ProductServiceImpl();
            PageBean<Product2> product2 = service.findProductByPage(currentPage, rows, condition);


            request.setAttribute("pb",product2);
            request.setAttribute("condition",condition);

            request.getRequestDispatcher("Index.jsp").forward(request,response);
        }

        if(action.equals("FindProductById")){
            String uid = request.getParameter("uid");
            String pid = request.getParameter("pid");

            UserService service = new UserServiceImpl();
            List<User> list = service.findUserById(Integer.parseInt(uid));
            User user = list.get(0);

            ProductService service1 = new ProductServiceImpl();
            List<Product> list1 = service1.FindProductById(Integer.parseInt(pid));
            Product product = list1.get(0);


            request.setAttribute("user",user);
            request.setAttribute("product",product);

            request.getRequestDispatcher("Comment/AddComment.jsp").forward(request,response);
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
